//
//  YMSMember.m
//  yameishe3
//
//  Created by pro on 2017/3/2.
//  Copyright © 2017年 yameishe. All rights reserved.
//

#import "YMSMember.h"

#define kPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"login.data"]

@implementation YMSMember
- (void)encodeWithCoder:(NSCoder *)encoder
{
    
    [encoder encodeObject:self.user_phone forKey:@"user_phone"];
    [encoder encodeObject:self.userid forKey:@"userid"];
    [encoder encodeObject:self.rst forKey:@"rst"];
    [encoder encodeObject:self.password forKey:@"password"];
    [encoder encodeObject:self.username forKey:@"username"];
    [encoder encodeObject:self.token forKey:@"token"];
    [encoder encodeObject:self.user_Eail forKey:@"user_Eail"];
    [encoder encodeObject:self.user_QQ forKey:@"user_QQ"];
    [encoder encodeObject:self.nickname forKey:@"nickname"];
    [encoder encodeObject:self.contact_person forKey:@"contact_person"];
    [encoder encodeObject:self.company_name forKey:@"company_name"];
}


- (id)initWithCoder:(NSCoder *)decoder
{
    
    
    
    if (self = [super init]) {
        self.user_phone = [decoder decodeObjectForKey:@"user_phone"];
        self.userid = [decoder decodeObjectForKey:@"userid"];
        self.username = [decoder decodeObjectForKey:@"username"];
        self.rst = [decoder decodeObjectForKey:@"rst"];
        self.password = [decoder decodeObjectForKey:@"password"];
        self.token = [decoder decodeObjectForKey:@"token"];
        self.user_Eail = [decoder decodeObjectForKey:@"user_Eail"];
        self.company_name = [decoder decodeObjectForKey:@"company_name"];
        self.contact_person = [decoder decodeObjectForKey:@"contact_person"];
        self.nickname = [decoder decodeObjectForKey:@"nickname"];
        self.user_QQ = [decoder decodeObjectForKey:@"user_QQ"];
 
    }
    return self;
}

static YMSMember* _loginMember;

+(instancetype)loginMember {
    if(!_loginMember) {
        _loginMember = [NSKeyedUnarchiver unarchiveObjectWithFile:kPath];
    }
    return _loginMember;
}


+ (void)setLoginMember:(YMSMember *)loginMember {
    _loginMember = loginMember;
    [NSKeyedArchiver archiveRootObject:_loginMember toFile:kPath];
    [[NSNotificationCenter defaultCenter] postNotificationName:kLoginNotificationName object:nil];
    //    if(loginMember==nil)
    //    {
    //    [APService setTags:nil
    //                 alias:nil
    //      callbackSelector:@selector(tagsAliasCallback:tags:alias:)
    //                target:self];
    //    }
    //    else
    //    {
    //        [APService setTags:nil
    //                     alias:[NSString stringWithFormat:@"%@",[ASKMember loginMember].userid]
    //          callbackSelector:@selector(tagsAliasCallback:tags:alias:)
    //                    target:self];
    //    }
}

+(void)saveLoginMember {
    [NSKeyedArchiver archiveRootObject:_loginMember toFile:kPath];
    [[NSNotificationCenter defaultCenter] postNotificationName:kUserInfoChangedNotificationName object:nil];
    
}


+(instancetype)memberWithKeyValues:(NSDictionary *)keyValues {
    YMSMember* member = [self objectWithKeyValues:keyValues];
    member.userid = [keyValues objectForKey:@"id"];
    return member;
}

-(instancetype)memberInitWithDic:(NSDictionary*)dic
{
    self.company_name = dic[@"company_name"];
    self.user_QQ = dic[@"user_QQ"];
    self.contact_person = dic[@"contact_person"];
    self.nickname = dic[@"nickname"];
    self.user_Eail = dic[@"user_Eail"];
    self.user_phone = dic[@"user_phone"];
    return self;
}
@end
